﻿using CommandProcessor.Command;
using Core.Common;
using Data.Infrastructure;
using Data.Repositories;
using Domain.Commands;
using Domain.Properties;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Data.Models;

namespace Domain.Handlers
{
    //Dung de check add
    public class CanAddOtherCostFormula : IValidationHandler<CreateOrUpdateOtherCostFormulaCommand>
    {
        private readonly IOtherCostFormulaRepository OtherCostFormulaRepository;
        public CanAddOtherCostFormula(IOtherCostFormulaRepository OtherCostFormulaRepository, IUnitOfWork unitOfWork)
        {
            this.OtherCostFormulaRepository = OtherCostFormulaRepository;
        }
        public IEnumerable<ValidationResult> Validate(CreateOrUpdateOtherCostFormulaCommand command)
        {
            //TODO
            //Neu Atributes (System.ComponentModel.DataAnnotations) khong thoa man cac dieu kien validation thi ban co
            //the them cac validation o day. Vd:
            if (command.Name == null)
                yield return new ValidationResult("Name", string.Format(Resources.Name));
        }
        public IEnumerable<ValidationResult> ValidateRange(IEnumerable<CreateOrUpdateOtherCostFormulaCommand> commands)
        {
            throw new NotImplementedException();
        }
    }

    //Dung de insert
    public class CreateOrUpdateOtherCostFormulaHandler : ICommandHandler<CreateOrUpdateOtherCostFormulaCommand>
    {
        private readonly IOtherCostFormulaRepository OtherCostFormulaRepository;
        private readonly IUnitOfWork unitOfWork;
        public CreateOrUpdateOtherCostFormulaHandler(IOtherCostFormulaRepository OtherCostFormulaRepository, IUnitOfWork unitOfWork)
        {
            this.OtherCostFormulaRepository = OtherCostFormulaRepository;
            this.unitOfWork = unitOfWork;
        }
        public ICommandResult Execute(CreateOrUpdateOtherCostFormulaCommand command)
        {
            var OtherCostFormula = new OtherCostFormula
            {
                ID = command.ID,
                Name = command.Name,
                Code = command.Code,
                UOMID = command.UOMID,
                DesignDetailID = command.DesignDetailID,
                ProductModelID = command.ProductModelID,
                Quantity = command.Quantity,
                OtherCostID=command.OtherCostID
            };
            if (OtherCostFormula.ID == 0)
                OtherCostFormulaRepository.Add(OtherCostFormula);
            else
                OtherCostFormulaRepository.Update(OtherCostFormula);

            unitOfWork.Commit();
            return new CommandResult(true);
        }
        public ICommandResult ExecuteRange(IEnumerable<CreateOrUpdateOtherCostFormulaCommand> commands)
        {
            List<OtherCostFormula> list = new List<OtherCostFormula>();
            foreach (var item in commands)
            {
                var company = new OtherCostFormula
                {
                    ID = item.ID,
                    Code = item.Code,
                    DesignDetailID = item.DesignDetailID,
                    OtherCostID = item.OtherCostID,
                    Name = item.Name,
                    ProductModelID = item.ProductModelID,
                    Quantity = item.Quantity,
                    UOMID = item.UOMID
                };
                list.Add(company);
            }
            if (list.Any(x => x.ID == 0))
                OtherCostFormulaRepository.AddRange(list.Where(x => x.ID == 0));
            else
                OtherCostFormulaRepository.UpdateRange(list.Where(x => x.ID != 0));
            unitOfWork.Commit();
            return new CommandResult(true);
        }
        public ICommandResult DeleteRange(IEnumerable<CreateOrUpdateOtherCostFormulaCommand> commands)
        {
            var ids = commands.Select(y => y.ID);
            OtherCostFormulaRepository.Delete(x => ids.Contains(x.ID));
            unitOfWork.Commit();
            return new CommandResult(true);
        }
    }

    //Dung de delete
    public class DeleteOtherCostFormulaHandler : ICommandHandler<DeleteOtherCostFormulaCommand>
    {
        private readonly IOtherCostFormulaRepository OtherCostFormulaRepository;
        private readonly IUnitOfWork unitOfWork;
        public DeleteOtherCostFormulaHandler(IOtherCostFormulaRepository OtherCostFormulaRepository, IUnitOfWork unitOfWork)
        {
            this.OtherCostFormulaRepository = OtherCostFormulaRepository;
            this.unitOfWork = unitOfWork;
        }
        public ICommandResult Execute(DeleteOtherCostFormulaCommand command)
        {
            var OtherCostFormula = OtherCostFormulaRepository.GetById(command.ID);
            OtherCostFormulaRepository.Delete(OtherCostFormula);
            unitOfWork.Commit();
            return new CommandResult(true);
        }


        public ICommandResult ExecuteRange(IEnumerable<DeleteOtherCostFormulaCommand> commands)
        {
            var ids = commands.Select(y => y.ID);
            OtherCostFormulaRepository.Delete(x => ids.Contains(x.ID));
            unitOfWork.Commit();
            return new CommandResult(true);
        }
        public ICommandResult DeleteRange(IEnumerable<DeleteOtherCostFormulaCommand> commands)
        {
            throw new NotImplementedException();
        }
    }
}

